#include "UIScreen.h"
#include <tchar.h>

UIScreen::UIScreen(UIControl* p_pParent) : UIControl(p_pParent)
{
}

UIScreen::~UIScreen()
{
	for (list<UIControl* >::iterator it = m_pControls.begin(); it != m_pControls.end(); it++)
	{
		delete *it;
	}
	m_pControls.clear();
}

void UIScreen::onDraw()
{
	m_pFocusing = 0;
	for (list<UIControl* >::iterator it = m_pControls.begin(); it != m_pControls.end(); it++)
	{
		if ((*it)->isFocus())
		{
			m_pFocusing = *it;
		}
		else
		{
			(*it)->onDraw();
		}
	}
	if (m_pFocusing)
	{
		m_pFocusing->onDraw();
	}
}

void UIScreen::onLostFocus()
{
	m_pFocusing->onLostFocus();
	m_pFocusing = 0;
	UIControl::onLostFocus();
}

void UIScreen::add(UIControl* p_pControl)
{
	if(p_pControl->getParent() && _tcscmp(p_pControl->getParent()->getType(), TEXT("UIScreen")) == 0)
	{
		((UIScreen)p_pControl->getParent())->remove(p_pControl);
	}
}

LPSTR UIScreen::getType()
{
	return TEXT("UIScreen");
}

void UIScreen::remove(UIControl* p_pControl)
{
	m_pControls.remove(p_pControl);
	p_pControl->setParent(NULL);
}

void UIScreen::setParent(UIControl* p_pParent)
{
	if(getParent() && _tcscmp(getParent()->getType(), TEXT("UIScreen")) == 0)
	{
		((UIScreen)getParent())->remove(this);
	}
	this->m_pParent = p_pParent;
}